package com.qf.servleProject.servlet.comtroller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ServletContextController",value = "/ctxController")
public class ServletContextController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//    1.通过 this.getServletContext()
        ServletContext servletContext = this.getServletContext();
//    2.通过 request 对象获取
//        ServletContext servletContext1 = request.getServletContext();
//    3.通过session 对象进行获取
//        HttpSession session = request.getSession();
//        ServletContext servletContext2 = session.getServletContext();

//        System.out.println(servletContext);
//        System.out.println(servletContext1);
//        System.out.println(servletContext2);

//      获取当前项目所在的路径  C:\Users\Administrator\Desktop\Tomcat项目\WebProject\out\artifacts\WebProject_war_exploded\
//        System.out.println(servletContext.getRealPath("/"));
//       获取当前项目的文件路劲  /WebProject_war_exploded
//        System.out.println(servletContext.getContextPath());
//        System.out.println(request.getContextPath());

//        设置为全局容器
        servletContext.setAttribute("context","info");



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
